﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using QuanLyTruongHoc_App.MyDataSet;

namespace QuanLyTruongHoc_App
{
    public partial class AddEditLopHoc : Form
    {
        private int currID = 0;
        private Boolean editMode = false;
        private DataRow editRow;
        private DataTable tmpGVTable;

        public AddEditLopHoc()
        {
            InitializeComponent();
        }

        private void bt_Sua_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddEditGiaoVien_Load(object sender, EventArgs e)
        {
            refreshData();
            refreshID();
        }

        private void refreshID()
        {
            if (!editMode)
                currID = PublicLib.getMaxIdFromTbl(qLTH.tbl_LopHoc.TableName, qLTH.tbl_LopHoc.idColumn.ColumnName) + 1;
        }

        private void refreshData()
        {
            // Change Connection String
            tbl_KhoiTableAdapter.Connection.ConnectionString =
                tbl_GiaoVienTableAdapter.Connection.ConnectionString =
                    tbl_PhongHocTableAdapter.Connection.ConnectionString =
                        tbl_CaHocTableAdapter.Connection.ConnectionString =
                            tbl_KieuLopTableAdapter.Connection.ConnectionString = 
                PublicLib.getStrConn();
            // Fill Data
            tbl_KhoiTableAdapter.FillByFlag(qLTH.tbl_Khoi, PConst.FLAG_NONE);
            tbl_GiaoVienTableAdapter.FillByFlag(qLTH.tbl_GiaoVien, PConst.FLAG_NONE);
            tbl_PhongHocTableAdapter.FillByFlag(qLTH.tbl_PhongHoc, PConst.FLAG_NONE);
            tbl_CaHocTableAdapter.FillByFlag(qLTH.tbl_CaHoc, PConst.FLAG_NONE);
            tbl_KieuLopTableAdapter.FillByFlag(qLTH.tbl_KieuLop, PConst.FLAG_NONE);

            tmpGVTable = new DataTable();
            tmpGVTable.Columns.Add("id");
            tmpGVTable.Columns.Add("TenDayDu");

            //cb_GiaoVien.Items.Clear();
            for (int i = 0; i < qLTH.tbl_GiaoVien.Rows.Count; i++)
            {
                //cb_GiaoVien.Items.Add(new ListItem
                int tmp = int.Parse(qLTH.tbl_GiaoVien.Rows[i][qLTH.tbl_GiaoVien.idColumn].ToString());
                tmpGVTable.Rows.Add(qLTH.tbl_GiaoVien.Rows[i][qLTH.tbl_GiaoVien.idColumn],
                                    PublicLib.formatGVId(tmp) + " - " + 
                                    qLTH.tbl_GiaoVien.Rows[i][qLTH.tbl_GiaoVien.HoDemColumn].ToString() + " " + 
                                    qLTH.tbl_GiaoVien.Rows[i][qLTH.tbl_GiaoVien.TenColumn].ToString());
            }

            cb_GiaoVien.DataSource = tmpGVTable;
            cb_GiaoVien.DisplayMember = "TenDayDu";
            cb_GiaoVien.ValueMember = "id";
            if (cb_GiaoVien.Items.Count > 0)
                cb_GiaoVien.SelectedIndex = 0;
        }

        public void setEditInfo(int _id)
        {
            try
            {
                DataRow[] newRow;
                MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter adap = 
                    new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();
                adap.Fill(qLTH.tbl_LopHoc);

                newRow = qLTH.tbl_LopHoc.Select(qLTH.tbl_LopHoc.idColumn.ColumnName + "=" + _id.ToString());
                if (newRow.Length == 0)
                    return;
                editRow = newRow[0];
                currID = _id;

                tb_MaLop.Text = editRow[qLTH.tbl_LopHoc.MaLopColumn].ToString();
                tb_TenLop.Text = editRow[qLTH.tbl_LopHoc.TenLopColumn].ToString();
                cb_KieuLop.SelectedValue = editRow[qLTH.tbl_LopHoc.KieuLopColumn];
                cb_CaHoc.SelectedValue = editRow[qLTH.tbl_LopHoc.CaHocColumn];
                cb_PhongHoc.SelectedValue = editRow[qLTH.tbl_LopHoc.PhongHocColumn];
                cb_GiaoVien.SelectedValue = editRow[qLTH.tbl_LopHoc.idGiaoVienColumn];
                cb_khoi.SelectedValue = editRow[qLTH.tbl_LopHoc.idKhoiColumn];

                editMode = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi :" + ex.Message);
            }
        }

        private Boolean checkInfo()
        {
            
            return true;
        }

        private void bt_ThemMoi_Click(object sender, EventArgs e)
        {
            if (!checkInfo())
                return;

            try
            {
                refreshID();
                if (!editMode)
                    editRow = qLTH.tbl_LopHoc.NewRow();

                editRow[qLTH.tbl_LopHoc.idColumn] = currID;
                editRow[qLTH.tbl_LopHoc.MaLopColumn] = tb_MaLop.Text;
                editRow[qLTH.tbl_LopHoc.TenLopColumn] = tb_TenLop.Text;
                editRow[qLTH.tbl_LopHoc.KieuLopColumn] = cb_KieuLop.SelectedValue;
                editRow[qLTH.tbl_LopHoc.CaHocColumn] = cb_CaHoc.SelectedValue;
                editRow[qLTH.tbl_LopHoc.PhongHocColumn] = cb_PhongHoc.SelectedValue;
                editRow[qLTH.tbl_LopHoc.idGiaoVienColumn] = cb_GiaoVien.SelectedValue;
                editRow[qLTH.tbl_LopHoc.idKhoiColumn] = cb_khoi.SelectedValue;

                editRow[qLTH.tbl_LopHoc.flagColumn] = PConst.FLAG_NONE;

                MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter adap = new QuanLyTruongHoc_App.MyDataSet.QLTHTableAdapters.tbl_LopHocTableAdapter();
                adap.Connection.ConnectionString = PublicLib.getStrConn();

                if (!editMode)
                    qLTH.tbl_LopHoc.Rows.Add(editRow);

                adap.Update(editRow);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(MSG.MSG_ERROR_INSERT_DB + " :" + ex.Message);
            }
        }
    }
}
